package com.cn.dao.impl;

import java.io.Serializable;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Component;

import com.cn.dao.UserDaoInterface;
import com.cn.domain.Users;

@Component("userDao")
public class UserDaoImpl implements UserDaoInterface {

	private SessionFactory sessionFactory;
	
	@Override
	public Serializable add(Users users) {
		// TODO Auto-generated method stub
		return getSession().save(users);
	}

	@Override
	public Users selUsers(String str1, String str2) {
		// TODO Auto-generated method stub
		List<Users> lists = null;
		try{ 
			Query query = getSession().createQuery("from Users u where u." + str1 + " = ?").setParameter(0, str2);
			lists = query.list();
		}catch(Exception e){
			e.printStackTrace();
		}
		if(lists == null) {
			return null;
		}
System.out.println("the lists size is " + lists.size());		
		if(lists.size() != 0) {
			return lists.get(0);
		} else {
			return null;
		}
	}

	@Override
	public List<Users> show() {
		// TODO Auto-generated method stub
		String sql = "from Users";
		Query query = this.sessionFactory.getCurrentSession().createQuery(sql);
		List<Users> list = query.list();
		return list;
	}
	
	public Session getSession() {
		return sessionFactory.getCurrentSession();
	}
	
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	@Resource
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

}
